< previous page page_297 next page >

Page 297
Listing 13.1 OPTIONARRAY.TXTUsing LBound and UBound to Determine the Range of a Control Array
Dim I As Integer, nChosen As Integer
For I = optArray.Lbound To optArray.Ubound
    If optArray(I).Value Then
        nChosen = I
        Exit Sub
    End If
Next
In Listing 13.1, you might notice that, instead of specifying exact values for the start and end of the For loop, the code uses the LBound and UBound properties. These properties return the lower and upper boundaries of the control array indexes, respectively. Using these properties allows your code to handle additions or deletions in the control array automatically. Lbound and Ubound are properties of the control array itself, not of individual members of the control array.
note.gif Although LBound and UBound provide the maximum and minimum values of the array indices, this does not mean that the elements of the array are numbered contiguously. square.gif

Parallel Arrays
One handy technique when working with control arrays involves the use of two (or more) different control arrays whose elements match with each other in some way. They are known as parallel arrays.
Consider the two control arrays in the sample program. One is a group of text boxes into which the users can enter information; the other is a group of label controls, each of which matches one of the text boxes. Suppose that you want the label that identifies whichever control has the focus to appear in boldface as a visual cue to the users to help spot the current control. You could accomplish this task by adding some code to the GotFocus event procedure for the TextBox control array elements; this code should set the Font.Bold property for the Label control that matches the TextBox control that has the focus. That is, when a given text box receives the focus, you want its associated label to become bold. You also should add some code to the TextBox control array's LostFocus event procedure to set the appropriate Label control's font back to normal.
To accomplish this enhancement, follow these steps:
1. Enter the following line of code into txtArray's GotFocus event procedure:
lblArray (Index).Font.Bold = True
Note that this code, although it is placed in an event procedure of the TextBox control array, actually acts on a member of the parallel Label control array. The Index parameter is used to determine which element of the Label control array to act on.

 
< previous page page_297 next page >